package com.scalar.chiptrack.operations.reports.actions;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.LabelValueBean;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.operations.release.dao.RelaseDAO;
import com.scalar.chiptrack.operations.dataentry.dao.ScrapMaterialDAO;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Sep 4, 2006
 * Time: 1:40:39 AM
 * To change this template use File | Settings | File Templates.
 */
public class WipInventoryReportAction extends ChipTrackAction
{
    private static final String ACTION_NAME = "/wipInventoryReport.do";
    public ActionForward execute ( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception
    {
        String action = request.getParameter( "action" );
        String processType = request.getParameter( "processType" );

        HttpSession session = request.getSession();
        User user = ( User ) session.getAttribute( SessionKeys.USER_KEY );

        if( user == null )
        {
            session.setAttribute( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString() );
            return ( mapping.findForward( "login" ) );
        }

        try
        {
            if ( action.equals( "inventoryInfoNew" ) )
            {
                String reportType = request.getParameter( "reportType" );

                if( processType == null || "0".equalsIgnoreCase ( processType ) )
                {
                   request.setAttribute( "mktgPartNumberSelectList", NewProductDAO.loadAllBaseProducts( null, true, true ) );
                    
                   if (reportType != null && reportType.equalsIgnoreCase( "finished" ) )
                   {
                       processType = Constants.LOCATION_TYPE_FG;
                   }
                   else
                   {
                       processType = "all";
                   }
                }
                else
                {
                   request.setAttribute("baseProcessType" , NewProductDAO.getBaseProcessName(null, processType )); 
                   request.setAttribute( "mktgPartNumberSelectList", NewProductDAO.loadPartNumbers( null, 0, processType, true, true, true, 0 ) );
                }

                if( reportType == null || reportType.equalsIgnoreCase( "wip" ) )
                {
                    request.setAttribute( "processList", NewProductDAO.loadWipProcessesFromDB( null, true) );
                }
                else if( reportType.equalsIgnoreCase( "inventory" ) )
                {
                    request.setAttribute( "processList", NewProductDAO.loadInventoryProcessesFromDB( null ) );
                }
                else if( reportType.equalsIgnoreCase( "intransit" ) )
                {
                    request.setAttribute( "processList", NewProductDAO.loadAllProcessesFromDB( null, true ) );  //For Transit Also loading all Process From DB
                }
                else
                {
                    request.setAttribute( "processList", NewProductDAO.loadAllProcessesFromDB( null,true ) );
                }
                //System.out.println( "reportType  "+ request.getParameter( "reportType" ) + " ==processType  "+ processType );
                request.setAttribute( "runInfoSelectList" , AdaptorRunInfo.getAdaptorRunInfoList() );
                request.setAttribute ( "vendorList" , VendorDAO.getVendorNamesByProcessByUserType( null, processType, user ) );
                request.setAttribute( "categoryList", getLotCategory () );
                request.setAttribute ( "statusList" , NewProductDAO.loadStatusList() );
            }
            else if ( action.equals( "inventoryInfo" ) )
            {
                String reportType = request.getParameter( "reportType" );

                if( processType == null || "0".equalsIgnoreCase ( processType ) )
                {
                   request.setAttribute( "mktgPartNumberSelectList", NewProductDAO.loadAllBaseProducts( null, true, true ) );
                     if ( reportType != null && reportType.equalsIgnoreCase( "finished" ) )
                     {
                        processType = Constants.LOCATION_TYPE_FG;
                     }
                     else
                     {
                        processType = "all";
                     }
                }
                else
                {
                   request.setAttribute( "mktgPartNumberSelectList", NewProductDAO.loadPartNumbers( null, 0, processType, true, true, true, 0 ) );
                }

                if( reportType == null || reportType.equalsIgnoreCase( "wip" ) )
                {
                    request.setAttribute( "processList", NewProductDAO.loadWipProcesses( null) );
                }
                else if( reportType.equalsIgnoreCase( "inventory" ) )
                {
                    request.setAttribute( "processList", NewProductDAO.loadInventoryProcesses( null ) );
                }
                else if( reportType.equalsIgnoreCase( "intransit" ) )
                {
                    request.setAttribute( "processList", NewProductDAO.loadAllProcesses( null ) );
                }
                else
                {
                    request.setAttribute( "processList", NewProductDAO.loadAllProcesses( null ) );
                }

                //System.out.println( "reportType  "+ request.getParameter( "reportType" ) + " ==processType  "+ processType );
                request.setAttribute( "runInfoSelectList" , AdaptorRunInfo.getAdaptorRunInfoList() );
                request.setAttribute ( "vendorList" , VendorDAO.getVendorNamesByProcessByUserType( null, processType, user ) );
                request.setAttribute( "categoryList", getLotCategory () );
                request.setAttribute ( "statusList" , NewProductDAO.loadStatusList() );
            }
            else if ( action.equals( "releaseLots" ) )
            {
                String psType = request.getParameter( "psType" );
                
                if( psType == null || "0".equalsIgnoreCase ( psType ) )
                {
                    request.setAttribute( "mktgPartNumberSelectList", new ArrayList() );
                }
                else
                {
                    request.setAttribute( "mktgPartNumberSelectList", NewProductDAO.loadPartNumbers( null, 0, psType, true, true, true, 0 ) );
                }
                request.setAttribute( "processList", loadReleaseProcess () );
                request.setAttribute( "toLocationList", VendorDAO.getVendorNamesByPsType( null, psType ) );
                request.setAttribute( "statusList", RelaseDAO.getAssmReleaseStatusList() );
                request.setAttribute( "requestTypes", RelaseDAO.getRequestTypes() );
                request.setAttribute( "categoryList", getLotCategory () );

            }
            else if ( "scrapLotsReport".equalsIgnoreCase(action) )
            {
                try
                {
                    if( processType == null || "0".equalsIgnoreCase ( processType ) )
                    {
                       request.setAttribute( "mktgPartNumberList", NewProductDAO.loadAllBaseProducts( null, true, true ));
                    }
                    else
                    {
                       request.setAttribute( "mktgPartNumberList", NewProductDAO.loadPartNumbers( null, 0, processType, true, true, true, 0 ) );
                    }

                    request.setAttribute ( "processList" , ScrapMaterialDAO.getScrapProcessTypes() );
                    request.setAttribute ( "vendorList" , VendorDAO.getVendorNamesByProcessByUserType (null,"all",null) );
                    request.setAttribute( "categoryList", getLotCategory () );
                    request.setAttribute ("scrapTypesList", getScrapTypesList());
                }
                catch( Exception e )
                {
                    e.printStackTrace();
                }
            }
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }

        if( action.equals( "inventoryInfo" ) )
        {
            return( mapping.findForward( "inventoryInfo" ) ) ;
        }
        else if( action.equals( "inventoryInfoNew" ) )
        {
            return( mapping.findForward( "inventoryInfoNew" ) ) ;
        }
        else if ( action.equals( "releaseLots" ) )
        {
            return( mapping.findForward( "releaseLots" ) ) ;
        }
        else if ("scrapLotsReport".equalsIgnoreCase(action))
        {
            return( mapping.findForward( "scrapLotsReport" ) ) ;
        }
        else if ("openPOReport".equalsIgnoreCase(action))
        {            
            return( mapping.findForward( "openPOReport" ) ) ;
        }
        else
        return null;
    }

    private ArrayList getLotCategory()
    {
        ArrayList categoryList = new ArrayList();

        categoryList.add( new LabelValueBean( "All Categories", "0") );
        categoryList.add( new LabelValueBean( "PROD", "PROD") );
        categoryList.add( new LabelValueBean( "ENGG", "ENGG" ) );

        return categoryList;
    }

    private ArrayList loadReleaseProcess()
    {
        ArrayList processList = new ArrayList();

        processList.add( new LabelValueBean( "Select Process", "0") );
        processList.add( new LabelValueBean( "Assembly", "Assembly") );
        processList.add( new LabelValueBean( "Test", "FT" ) );

        return processList;
    }

    private ArrayList getScrapTypesList()
    {
        ArrayList scrapTypesList = new ArrayList();

        scrapTypesList.add( new LabelValueBean( "All ", "2") );
        scrapTypesList.add( new LabelValueBean( "SCRAPED", "1") );
        scrapTypesList.add( new LabelValueBean( "Non Valued", "0" ) );

        return scrapTypesList;
    }
}
